<template>
	<view class="container" style="padding-bottom: 120rpx;">
		<u--form ref="queryForm" :model="query">
			<u-form-item label="" prop="materialCode" style="display: flex;">
				<span style="margin-right: 20rpx;width: 120rpx;">物料码</span>
				<u--input
					placeholder="请输入/扫描"
					v-model="query.materialCode" 
					suffixIcon="scan" 
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;"
				>
				</u--input>
			</u-form-item>
			<u-form-item label="" prop="materialBatch" style="display: flex;">
				<span style="margin-right: 20rpx;width: 120rpx;">批次号</span>
				<u--input
					placeholder="请输入/扫描" 
					v-model="query.materialBatch" 
					suffixIcon="scan"
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;"
				>
				</u--input>
			</u-form-item>
			<u-form-item label="" prop="containerCode" style="display: flex;">
				<span style="margin-right: 20rpx;width: 120rpx;">容器码</span>
				<u--input
					placeholder="请输入/扫描"
					v-model="query.containerCode" 
					suffixIcon="scan" 
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;"
					@confirm="getDetail()"
				>
				</u--input>
			</u-form-item>
			<u-form-item label="" prop="quantity" style="display: flex;">
				<span style="margin-right: 20rpx;width: 120rpx;">输入数量</span>
				<u--input
					type="number"
					:disabled="flag"
					placeholder="请输入"
					v-model="query.quantity"
					style="flex: 1;"
				>
				</u--input>
			</u-form-item>
		</u--form>
		<view style="display: flex;flex-direction: column;margin: 10rpx 0;" class="boxDetail">
			<!-- 一格、两格 -->
			<view v-if="isolationArea === 1 || isolationArea === 2" style="display: flex;flex-direction: column;gap: 20rpx;width: 100%;">
				<view v-for="(item, index) in list" :key="index">
					<view style="display: flex;height: 180rpx;">
						<view style="border: #53c21c 2rpx solid;font-size: 30rpx;flex-direction: column;flex: 8;background-color: #a9e08f;display: flex;align-items: center;justify-content: center;gap: 5rpx;">
							<view>空间号:{{ item.boxNo }}</view>
							<view style="display: flex;gap: 20rpx;">
								<view>物料编码: {{ item.materialCode }}</view>
								<view>数量: {{ item.materialQty }}</view>
							</view>
							<view style="display: flex;align-items: center;">
								<view style="margin-right: 10rpx;">入库数量:</view>
								<u--input
									placeholder="请输入数量" 
									v-model="item.quantity"
									style="flex: 1;background-color: #fff;width: 150rpx;"
								>
								</u--input>
							</view>
						</view>
					</view>
				</view>
			</view>
			<!-- 四格 -->
			<view v-else style="display: flex;flex-wrap: wrap;justify-content: space-around;gap: 10rpx;">
				<view v-for="(item, index) in list" :key="index" style="flex: 1;box-sizing: border-box;">
					<view style="display: flex;height: auto;">
						<view style="box-sizing: border-box;padding: 15rpx; border: #53c21c 2rpx solid;font-size: 28rpx;flex-direction: column;flex: 5;background-color: #a9e08f;display: flex;align-items: center;gap: 5rpx;">
							<view>空间号:{{ item.boxNo }}</view>
							<view style="display: flex;gap: 10rpx;">
								<view>物料编码: {{ item.materialCode }}</view>
								<view>数量: {{ item.materialQty }}</view>
							</view>
							<view style="display: flex;align-items: center;">
								<view style="margin-right: 10rpx;">数量:</view>
								<u--input
									type="number"
									placeholder="请输入数量" 
									v-model="item.quantity"
									style="flex: 1;background-color: #fff;width: 150rpx;"
								>
								</u--input>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view style="display: flex;gap: 20rpx;position: fixed;bottom: 0;left: 0;right: 0;padding: 10rpx;">
			<u-button type="primary" @click="emergency">紧急发料</u-button>
			<u-button type="warning" @click="reset">重置</u-button>
		</view>
		 <u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import { emergencyAPI } from '@/api/work/inbound.js'
	import { getDetailAPI } from '@/api/work/inbound.js'
	export default {
		data() {
			return {
				query: {
					materialCode: '',
					materialBatch: '',
					containerCode: '',
					quantity: null
				},
				list: [],
				isolationArea: null,
				flag: false
			};
		},
		watch: {
		  query: {
		    handler(newVal) {
					if (newVal.containerCode) {
						this.query.quantity = ''
						this.flag = true
					} else {
						this.flag = false
					}
		    },
		    deep: true // 深度监听
		  }
		},
		onLoad(options) {
			uni.$on('scancodedate',(data) => this.getCode(data.code))
		},
		onUnload() {  
		  // 移除监听事件
		  uni.$off('scancodedate')
		},
		methods: {
			getCode(code) {
				const pattern = /^[A-Za-z]\d{9}$/
				if (pattern.test(code)) {
					this.query.containerCode = code
					this.getDetail()
				} else if (code.length === 18) {
					this.query.materialCode = code
				} else if (code.length > 0) {
					this.materialBatch = code
				}
			},
			async emergency() {
				if (!this.query.materialCode) {
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请输入/扫描物料码'
					})
				}
				if (this.query.materialCode && !this.query.materialBatch) {
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请输入/扫描批次号'
					})
				}
				if (!this.flag) {
					if (this.query.materialCode && this.query.materialBatch && this.query.containerCode && !this.query.quantity) {
						return this.$refs.uToast.show({
							type: 'warning',
							message: '请填写数量'
						})
					}
					if (!/^\d+$/.test(this.query.quantity)) {
						return this.$refs.uToast.show({
							type: 'warning',
							message: '数量必须为数字且大于等于0'
						})
					}
				} else {
					if (this.query.materialCode && this.query.materialBatch && !this.query.containerCode) {
						return this.$refs.uToast.show({
							type: 'warning',
							message: '请输入/扫描容器码'
						})
					}
				}
				const res = await emergencyAPI(this.query)
				if (res.meta.code === 200) {
					this.$refs.uToast.show({
						type: 'success',
						message: res.meta.msg
					})
					this.reset()
				} else {
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.meta.msg
					})
				}
			},
			reset() {
				this.query = {
					materialCode: '',
					materialBatch: '',
					containerCode: '',
					quantity: null
				}
				this.list = []
				this.isolationArea = null
				this.flag = false
			},
			async getDetail() {
				const res = await getDetailAPI(this.query)
				if (res.meta.code === 200) {
					this.list = res.data.containerResp.containerDetails.map(item => {
						return {
							...item,
							quantity: null
						}
					})
					this.isolationArea = res.data.containerResp.container.isolationArea
				} else {
					this.list = []
					this.isolationArea = 0
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.meta.msg
					})
				}
			}
		}
	}
</script>

<style lang="scss">

</style>
